/*
 * $Id: PreferencesTreeNode.java,v 1.6 2002/12/07 18:02:19 jppietsch Exp $
 * Copyright (C) 2002 Jan-Peter Pietsch
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.jppietsch.prefedit;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

/**
 * A tree node model of a {@link Preferences} object.
 *
 * @author Jan-Peter Pietsch
 * @version $Revision: 1.6 $
 */
public class PreferencesTreeNode extends DefaultMutableTreeNode {

    // Fields

    /**
     * The name of this tree node.
     */
    private final String name;

    /**
     * Preferences which are represented by this tree node.
     */
    private final Preferences preferences;

    // Constructors

    /**
     * Constructs a tree node for a given {@link Preferences} object.
     *
     * @param somePreferences To be viewed as tree node.
     * @param aDefaultNameKey which is used to retrieve a name from {@link Rsc} that is
     *                        displayed if the preferences name is empty.
     */
    public PreferencesTreeNode(Preferences somePreferences,
                               String aDefaultNameKey) {
        String[] childrenNames;

        try {
            childrenNames = somePreferences.childrenNames();
        } catch (BackingStoreException e) {
            e.printStackTrace();
            childrenNames = new String[0];
        }

        preferences = somePreferences;

        if (preferences.name().length() > 0) {
            name = preferences.name();
        } else {
            name = Rsc.getBundle().getString(aDefaultNameKey);
        }

        for (int i = 0; i < childrenNames.length; i++) {
            add(new PreferencesTreeNode(preferences.node(childrenNames[i]),
                    aDefaultNameKey));
        }
    }

    /**
     * Returns the preferences node which is represented by this tree node.
     *
     * @return Preferences which are represented by this tree node.
     */
    public Preferences getPreferences() {
        return preferences;
    }

    /**
     * Description of the Method
     *
     * @return Description of the Returned Value
     */
    public String toString() {
        return name;
    }

}
